package com.meterreadingsys.db;

import java.util.List;

import junit.framework.Test;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class UserInfoDB {

	

	public SQLiteDatabase db;

	public UserInfoDB(SQLiteDatabase db) {
		this.db=db;
	}

	/**
	 * 批量插入客户信息
	 * 
	 * @param list
	 * @return
	 */
	public boolean insertAllUser(List<ContentValues> list) {

		
		db.beginTransaction();
		for (int i = 0; i < list.size(); i++) {
			long id = db.insert("userinfo", null, list.get(i));

			if (id < 1) {
				return false;
			}
		}
		db.setTransactionSuccessful();
		db.endTransaction();
		
		return true;

	}

	/**
	 * 修改客户信息
	 * 
	 * @param values
	 * @param id
	 * @return
	 */
	public boolean modifyUserInfo(ContentValues values, int id) {
		
		int i = db.update("userinfo", values, "_id=?", new String[] { id + "" });
		

		if (i < 0) {
			return false;
		}
		return true;

	}

	/**
	 * 查询一个客户信息
	 * 
	 * @param user
	 * @return
	 */
	public UserInfo queryUserInfo(UserInfo user,String tag){
		  
		
		  String selection= "";
		  String order="";
		  if(tag.equals("next")){
			
			  selection="_id>?";
		  }else if(tag.equals("modify")){
			  selection="_id=?";
		  } else{
			  order=" _id desc";
			  selection="_id<?";
			 
		  }
		   UserInfo userinfo=null;
		 
		  
		  
		  String[] selectionArgs={user.getId()+""};
		  Cursor cursor=  db.query("userinfo", new String[]{"_id,username,userpass,useradds,phone"}, selection, selectionArgs, null, null, order);
		 
		   if(cursor.moveToNext()){
			   userinfo=new UserInfo();
			   userinfo.setId(cursor.getInt(cursor.getColumnIndex("_id")));
			   userinfo.setName(cursor.getString(cursor.getColumnIndex("username")));
			   userinfo.setAdds(cursor.getString(cursor.getColumnIndex("useradds")));
			   userinfo.setPhone(cursor.getString(cursor.getColumnIndex("phone")));
			   userinfo.setPass(cursor.getString(cursor.getColumnIndex("userpass")));
		   }
		  
		
		  
		return userinfo;
		  
	  }

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	public boolean delectUserInfo(int id) {
	
		int i = db.delete("userinfo", "_id=?", new String[] { id + "" });
	
		if (i > 0) {
			return true;
		}
		return false;

	}
	
	/**
	 * 删除所有用户数据
	 */
	public void delectAll(){
	
		  db.delete("userinfo", "_id>?", new String[]{"0"});
	}
	
	public void delectAllTest(){
		
		  db.delete("testfrom", "_id>?", new String[]{"0"});
	}
	
	/**
	 * 查询用户历史用水信息
	 * @param id
	 * @return
	 */
	public Cursor query(String id){
		 Cursor cursor=null;
		 
		  
		 String sql="select * from testfrom where num=?";
		 cursor=db.rawQuery(sql, new String[]{id});
	
		return cursor;
		
	}
	
	
	 public boolean insert(List<ContentValues> list){
		
			db.beginTransaction();
			for (int i = 0; i < list.size(); i++) {
				long id = db.insert("testfrom", null, list.get(i));

				if (id < 1) {
					return false;
				}
			}
			db.setTransactionSuccessful();
			db.endTransaction();
	
		return false;
		 
	 }
}
